package com.dmwork.service.work.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dmwork.common.base.result.R;
import com.dmwork.service.work.entity.Work;
import com.dmwork.service.work.entity.WorkType;
import com.dmwork.service.work.entity.WorkTypeDescription;
import com.dmwork.service.work.entity.vo.WorkTypeBriefInfoVo;
import com.dmwork.service.work.entity.vo.WorkTypeDetailInfoVo;
import com.dmwork.service.work.service.WorkService;
import com.dmwork.service.work.service.WorkTypeDescriptionService;
import com.dmwork.service.work.service.WorkTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-03-14
 */
@Transactional
@RestController
@RequestMapping("/admin/dm_work/work/work_type")
public class AdminWorkTypeController {

    @Autowired
    private WorkTypeService workTypeService;
    @Autowired
    private WorkTypeDescriptionService workTypeDescriptionService;
    @Autowired
    private WorkService workService;

    @ApiOperation("添加作品类型")
    @PostMapping("add_type")
    public R addType(@RequestBody WorkTypeDetailInfoVo workTypeDetailInfoVo) {
        WorkType workType = new WorkType();
        workType.setId(workTypeDetailInfoVo.getId());
        workType.setName(workTypeDetailInfoVo.getName());
        workType.setCover(workTypeDetailInfoVo.getCover());
        workTypeService.save(workType);
        WorkTypeDescription workTypeDescription = new WorkTypeDescription();
        workTypeDescription.setId(workTypeDetailInfoVo.getId());
        workTypeDescription.setDescription(workTypeDetailInfoVo.getDescription());
        workTypeDescriptionService.save(workTypeDescription);
        return R.ok().message("保存成功");
    }

    @ApiOperation("修改作品类型信息")
    @PutMapping("modify_info")
    public R modifyInfo(@RequestBody WorkTypeDetailInfoVo workTypeDetailInfoVo) {
        WorkType workType = new WorkType();
        WorkTypeDescription workTypeDescription = new WorkTypeDescription();
        BeanUtils.copyProperties(workTypeDetailInfoVo, workType);
        BeanUtils.copyProperties(workTypeDetailInfoVo, workTypeDescription);
        workTypeService.updateById(workType);
        workTypeDescriptionService.updateById(workTypeDescription);
        return R.ok().message("修改成功");
    }

    @ApiOperation("删除指定作品类型")
    @DeleteMapping("remove_type/{typeId}")
    public R removeType(@PathVariable String typeId) {
        workTypeService.removeById(typeId);
        workTypeDescriptionService.removeById(typeId);
        //TODO 删除所有该类型的作品
        
        return R.ok().message("删除成功");
    }

    @ApiOperation("获取作品类型的简略信息列表")
    @GetMapping("get_brief_info_list")
    public R getBriefInfoList() {
        List<WorkType> workTypeList = workTypeService.list();
        List<WorkTypeBriefInfoVo> workTypeBriefInfoVoList=new ArrayList<>(workTypeList.size());
        for (WorkType workType : workTypeList) {
            WorkTypeBriefInfoVo workTypeBriefInfoVo = new WorkTypeBriefInfoVo();
            BeanUtils.copyProperties(workType, workTypeBriefInfoVo);
            workTypeBriefInfoVoList.add(workTypeBriefInfoVo);
        }
        return R.ok().data("items", workTypeBriefInfoVoList);
    }

    @ApiOperation("获取作品类型信息列表")
    @GetMapping("get_info_list")
    public R getInfoList() {
        List<WorkType> workTypeList = workTypeService.list();
        List<WorkTypeBriefInfoVo> workTypeBriefInfoVoList=new ArrayList<>(workTypeList.size());
        for (WorkType workType : workTypeList) {
            WorkTypeBriefInfoVo workTypeBriefInfoVo = new WorkTypeBriefInfoVo();
            BeanUtils.copyProperties(workType, workTypeBriefInfoVo);
            QueryWrapper<Work> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type_id", workType.getId());
            int count = workService.count(queryWrapper);
            workTypeBriefInfoVo.setWorkCount(count);
            workTypeBriefInfoVoList.add(workTypeBriefInfoVo);
        }
        return R.ok().data("items", workTypeBriefInfoVoList);
    }

    @ApiOperation("获取指定作品类型的信息")
    @GetMapping("get_info/{typeId}")
    public R getInfo(@PathVariable String typeId) {
        WorkTypeDetailInfoVo workTypeDetailInfoVo = workTypeService.getDetailInfo("admin", typeId);
        return R.ok().data("item", workTypeDetailInfoVo);
    }
}

